namespace QMVnSoft.Controls
{
    using QMVnSoft;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.Properties;
    using QMVnSoft.SystemForms;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormListLister : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnDelete;
        private ToolStripButton btnDone;
        private ToolStripButton btnEdit;
        private ToolStripButton btnMerge;
        private ToolStripButton btnPreview;
        private ToolStripButton btnPrint;
        private ToolStripButton btnRefresh;
        private IContainer components;
        private GridControl grdBrowser;
        private bool ib_AllowAddItem;
        private bool ib_AllowDeleteItem;
        private bool ib_AllowEditItem;
        private bool ib_AllowMergeItem;
        private bool ib_CheckLookup;
        private bool ib_CheckLookupChild;
        private bool ib_EmptyIsValid;
        private bool ib_EnterKeyHook;
        private bool ib_EscapeKeyHook;
        private bool ib_IsChildForm;
        private bool ib_IsParentForm;
        private bool ib_RefreshAfterChanged;
        private bool ib_ShowAddButton;
        private bool ib_ShowAditButton;
        private bool ib_ShowDeleteButton;
        private bool ib_ShowDoneButton;
        private bool ib_ShowFreeFields;
        private bool ib_ShowMergeButton;
        private bool ib_SortByColumn;
        private int ii_FieldCount;
        private string is_ChildFirstFilter;
        private string is_ChildItemCode;
        private object[][] is_ColNames;
        private string is_CurrentCode;
        private string is_FieldName;
        private string is_FirstFilter;
        private string is_LocalKeyField;
        private string is_LocalTableName;
        private string is_OrderFieldNames;
        private FormListLister oChildFormLister;
        private DataRow oCurrentRow;
        private FormListEditor oFormEditor;
        private DataSet oLocalDataSet;
        private FormListLister oParentFormLister;
        private object oUserDefined1;
        private object oUserDefined2;
        private object oUserDefined3;
        private object oUserDefined4;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStripSeparator tbrSep04;
        private ToolStripSeparator tbrSep05;
        private ToolStripSeparator tbrSep06;
        private ToolStripSeparator tbrSep07;
        private ToolStrip Toolbars;
        private TbarSearch txtSearch;

        public FormListLister()
        {
            this.ib_ShowAddButton = true;
            this.ib_ShowAditButton = true;
            this.ib_ShowDeleteButton = true;
            this.ib_ShowMergeButton = true;
            this.ib_ShowFreeFields = true;
            this.is_LocalKeyField = "Stt";
            this.ib_EmptyIsValid = true;
            this.is_CurrentCode = "";
            this.ib_SortByColumn = true;
            this.is_FirstFilter = "";
            this.is_LocalTableName = "";
            this.is_OrderFieldNames = "";
            this.is_FieldName = "";
            this.ii_FieldCount = -1;
            this.is_ChildFirstFilter = "";
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.AllowUserToOrderColumns = this.SortByColumn;
            this.OnFillData();
        }

        public FormListLister(FormListLister po_ParentForm)
        {
            this.ib_ShowAddButton = true;
            this.ib_ShowAditButton = true;
            this.ib_ShowDeleteButton = true;
            this.ib_ShowMergeButton = true;
            this.ib_ShowFreeFields = true;
            this.is_LocalKeyField = "Stt";
            this.ib_EmptyIsValid = true;
            this.is_CurrentCode = "";
            this.ib_SortByColumn = true;
            this.is_FirstFilter = "";
            this.is_LocalTableName = "";
            this.is_OrderFieldNames = "";
            this.is_FieldName = "";
            this.ii_FieldCount = -1;
            this.is_ChildFirstFilter = "";
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.AllowUserToOrderColumns = this.SortByColumn;
            this.IsChildForm = true;
            this.ParentFormLister = po_ParentForm;
            this.CheckLookup = this.ParentFormLister.CheckLookup;
            this.ParentCurrentRow = this.ParentFormLister.LocalCurrentRow;
        }

        public FormListLister(object poUserDefined1, object poUserDefined2)
        {
            this.ib_ShowAddButton = true;
            this.ib_ShowAditButton = true;
            this.ib_ShowDeleteButton = true;
            this.ib_ShowMergeButton = true;
            this.ib_ShowFreeFields = true;
            this.is_LocalKeyField = "Stt";
            this.ib_EmptyIsValid = true;
            this.is_CurrentCode = "";
            this.ib_SortByColumn = true;
            this.is_FirstFilter = "";
            this.is_LocalTableName = "";
            this.is_OrderFieldNames = "";
            this.is_FieldName = "";
            this.ii_FieldCount = -1;
            this.is_ChildFirstFilter = "";
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.AllowUserToOrderColumns = this.SortByColumn;
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            this.OnFillData();
        }

        public FormListLister(object poUserDefined1, object poUserDefined2, object poUserDefined3)
        {
            this.ib_ShowAddButton = true;
            this.ib_ShowAditButton = true;
            this.ib_ShowDeleteButton = true;
            this.ib_ShowMergeButton = true;
            this.ib_ShowFreeFields = true;
            this.is_LocalKeyField = "Stt";
            this.ib_EmptyIsValid = true;
            this.is_CurrentCode = "";
            this.ib_SortByColumn = true;
            this.is_FirstFilter = "";
            this.is_LocalTableName = "";
            this.is_OrderFieldNames = "";
            this.is_FieldName = "";
            this.ii_FieldCount = -1;
            this.is_ChildFirstFilter = "";
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.AllowUserToOrderColumns = this.SortByColumn;
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            this.UserDefined3 = poUserDefined3;
            this.OnFillData();
        }

        public FormListLister(object poUserDefined1, object poUserDefined2, object poUserDefined3, object poUserDefined4)
        {
            this.ib_ShowAddButton = true;
            this.ib_ShowAditButton = true;
            this.ib_ShowDeleteButton = true;
            this.ib_ShowMergeButton = true;
            this.ib_ShowFreeFields = true;
            this.is_LocalKeyField = "Stt";
            this.ib_EmptyIsValid = true;
            this.is_CurrentCode = "";
            this.ib_SortByColumn = true;
            this.is_FirstFilter = "";
            this.is_LocalTableName = "";
            this.is_OrderFieldNames = "";
            this.is_FieldName = "";
            this.ii_FieldCount = -1;
            this.is_ChildFirstFilter = "";
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.grdBrowser.AllowUserToOrderColumns = this.SortByColumn;
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            this.UserDefined3 = poUserDefined3;
            this.UserDefined4 = poUserDefined4;
            this.OnFillData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!this.AllowAddItem)
            {
                MessageBox.Show("Không có quyền thêm mới danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.OnAdd();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!this.AllowDeleteItem)
            {
                MessageBox.Show("Không có quyền xóa danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.OnDelete();
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            this.OnDone();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!this.AllowEditItem)
            {
                MessageBox.Show("Không có quyền sửa danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.OnEdit();
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (this.ShowMergeButton)
            {
                if (!this.AllowMergeItem)
                {
                    MessageBox.Show("Không có quyền gộp mã!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    this.OnMerge();
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.OnRefresh();
            if (!this.OrderFieldNames.Equals(string.Empty))
            {
                ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        [Browsable(false)]
        public string GetCurrentData(string ps_FieldName)
        {
            if (this.LocalCurrentRow != null)
            {
                return this.LocalCurrentRow[ps_FieldName].ToString();
            }
            return "";
        }

        private void grdBrowser_DoubleClick(object sender, EventArgs e)
        {
            if (this.AllowEditItem)
            {
                this.OnEdit();
            }
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormListLister));
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.tbrSep05 = new System.Windows.Forms.ToolStripSeparator();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.btnEdit = new System.Windows.Forms.ToolStripButton();
            this.btnDelete = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.btnDone = new System.Windows.Forms.ToolStripButton();
            this.tbrSep07 = new System.Windows.Forms.ToolStripSeparator();
            this.txtSearch = new QMVnSoft.Controls.TbarSearch();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnMerge = new System.Windows.Forms.ToolStripButton();
            this.tbrSep04 = new System.Windows.Forms.ToolStripSeparator();
            this.btnPreview = new System.Windows.Forms.ToolStripButton();
            this.btnPrint = new System.Windows.Forms.ToolStripButton();
            this.tbrSep06 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.grdBrowser = new QMVnSoft.Controls.GridControl();
            this.Toolbars.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).BeginInit();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnRefresh,
            this.tbrSep05,
            this.btnAdd,
            this.btnEdit,
            this.btnDelete,
            this.tbrSep01,
            this.btnDone,
            this.tbrSep07,
            this.txtSearch,
            this.tbrSep02,
            this.btnMerge,
            this.tbrSep04,
            this.btnPreview,
            this.btnPrint,
            this.tbrSep06,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(794, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnRefresh
            // 
            this.btnRefresh.Image = global::QMVnSoft.Properties.Resources.REFRESH;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(66, 22);
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.ToolTipText = "Lấy lại dữ liệu";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // tbrSep05
            // 
            this.tbrSep05.Name = "tbrSep05";
            this.tbrSep05.Size = new System.Drawing.Size(6, 25);
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::QMVnSoft.Properties.Resources.BALANCE1;
            this.btnAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm mới (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnEdit
            // 
            this.btnEdit.Image = global::QMVnSoft.Properties.Resources.EDIT1;
            this.btnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(46, 22);
            this.btnEdit.Text = "Sửa";
            this.btnEdit.ToolTipText = "Sửa (F3)";
            this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
            // 
            // btnDelete
            // 
            this.btnDelete.Image = global::QMVnSoft.Properties.Resources.DELETE1;
            this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(47, 22);
            this.btnDelete.Text = "Xóa";
            this.btnDelete.ToolTipText = "Xoá (F8)";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // btnDone
            // 
            this.btnDone.Image = global::QMVnSoft.Properties.Resources.DONE;
            this.btnDone.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDone.Name = "btnDone";
            this.btnDone.Size = new System.Drawing.Size(80, 22);
            this.btnDone.Text = "Thực hiện";
            this.btnDone.ToolTipText = "Thực hiện (F10)";
            this.btnDone.Visible = false;
            this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
            // 
            // tbrSep07
            // 
            this.tbrSep07.Name = "tbrSep07";
            this.tbrSep07.Size = new System.Drawing.Size(6, 25);
            this.tbrSep07.Visible = false;
            // 
            // txtSearch
            // 
            this.txtSearch.Name = "txtSearch";
            this.txtSearch.Size = new System.Drawing.Size(100, 25);
            this.txtSearch.Text = "Tìm nhanh...";
            this.txtSearch.ToolTipText = "F4";
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnMerge
            // 
            this.btnMerge.Image = global::QMVnSoft.Properties.Resources.MERGE1;
            this.btnMerge.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnMerge.Name = "btnMerge";
            this.btnMerge.Size = new System.Drawing.Size(69, 22);
            this.btnMerge.Text = "Gộp mã";
            this.btnMerge.ToolTipText = "Gộp mã (F6)";
            this.btnMerge.Click += new System.EventHandler(this.btnMerge_Click);
            // 
            // tbrSep04
            // 
            this.tbrSep04.Name = "tbrSep04";
            this.tbrSep04.Size = new System.Drawing.Size(6, 25);
            // 
            // btnPreview
            // 
            this.btnPreview.Image = global::QMVnSoft.Properties.Resources.PREVIEW1;
            this.btnPreview.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPreview.Name = "btnPreview";
            this.btnPreview.Size = new System.Drawing.Size(51, 22);
            this.btnPreview.Text = "Xem";
            this.btnPreview.ToolTipText = "Xem trước khi in (Ctrl+F7)";
            // 
            // btnPrint
            // 
            this.btnPrint.Image = global::QMVnSoft.Properties.Resources.PRINT1;
            this.btnPrint.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(37, 22);
            this.btnPrint.Text = "In";
            this.btnPrint.ToolTipText = "In (F7)";
            // 
            // tbrSep06
            // 
            this.tbrSep06.Name = "tbrSep06";
            this.tbrSep06.Size = new System.Drawing.Size(6, 25);
            this.tbrSep06.Visible = false;
            // 
            // btnClose
            // 
            this.btnClose.Image = ((System.Drawing.Image)(resources.GetObject("btnClose.Image")));
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Visible = false;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // grdBrowser
            // 
            this.grdBrowser.AllowUserToAddRows = false;
            this.grdBrowser.AllowUserToDeleteRows = false;
            this.grdBrowser.CheckColor = false;
            this.grdBrowser.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.grdBrowser.Font = new System.Drawing.Font("Arial", 8.25F);
            this.grdBrowser.Location = new System.Drawing.Point(0, 25);
            this.grdBrowser.Name = "grdBrowser";
            this.grdBrowser.Size = new System.Drawing.Size(794, 514);
            this.grdBrowser.TabIndex = 1;
            this.grdBrowser.DoubleClick += new System.EventHandler(this.grdBrowser_DoubleClick);
            // 
            // FormListLister
            // 
            this.ClientSize = new System.Drawing.Size(794, 539);
            this.Controls.Add(this.grdBrowser);
            this.Controls.Add(this.Toolbars);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimumSize = new System.Drawing.Size(802, 566);
            this.Name = "FormListLister";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public virtual bool OnAdd()
        {
            if (this.ShowAddButton && (this.LocalFormEditor != null))
            {
                this.LocalFormEditor.LocalFormLister = this;
                this.LocalFormEditor.NewChecked = true;
                this.LocalFormEditor.LocalCurrentRow = this.LocalCurrentRow;
                this.LocalFormEditor.Show();
            }
            return true;
        }

        public virtual bool OnDelete()
        {
            if (this.ShowDeleteButton)
            {
                if ((this.LocalGridBrowser.SelectedRows == null) || (this.LocalGridBrowser.SelectedRows.Count == 0))
                {
                    return false;
                }
                if (MessageBox.Show(AppInstance.DeleMessage, AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return false;
                }
                foreach (DataGridViewRow row in this.LocalGridBrowser.SelectedRows)
                {
                    DataTable table = AppDataAccess.CheckInUsed(this.LocalFieldName, this.LocalCurrentRow[this.LocalFieldName]);
                    if (table.Rows.Count != 0)
                    {
                        FormCheckDeleted deleted = new FormCheckDeleted();
                        deleted.LocalTable = table;
                        deleted.ShowDialog();
                        return false;
                    }
                    DataRow row2 = ((DataRowView) row.DataBoundItem).Row;
                    if (AppDataAccess.Delete(this.LocalTableName, row2[this.LocalKeyField]))
                    {
                        this.LocalTable.Rows.Remove(row2);
                    }
                }
                this.LocalTable.AcceptChanges();
            }
            return true;
        }

        public virtual void OnDone()
        {
            MessageBox.Show(AppInstance.EndMessage, AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        public virtual bool OnEdit()
        {
            if (this.ShowEditButton)
            {
                if ((this.LocalTable == null) || (this.LocalTable.Rows.Count == 0))
                {
                    return false;
                }
                if (this.LocalFormEditor != null)
                {
                    this.LocalFormEditor.LocalFormLister = this;
                    this.LocalFormEditor.NewChecked = false;
                    this.LocalFormEditor.LocalCurrentRow = this.LocalCurrentRow;
                    this.LocalFormEditor.Show();
                }
            }
            return true;
        }

        public virtual void OnFillData()
        {
            if ((this.ColNames != null) && (this.ColNames.GetLength(0) != 0))
            {
                this.OnRefresh();
                if ((this.LocalTable != null) && !this.OrderFieldNames.Equals(string.Empty))
                {
                    ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
                }
                for (int i = 0; i < this.ColNames.GetLength(0); i++)
                {
                    if (((string) this.ColNames[i][2]) == "C")
                    {
                        DataGridViewTextBoxColumn dataGridViewColumn = new DataGridViewTextBoxColumn();
                        dataGridViewColumn.DataPropertyName = (string) this.ColNames[i][0];
                        dataGridViewColumn.HeaderText = (string) this.ColNames[i][1];
                        if (!this.SortByColumn)
                        {
                            dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
                        }
                        dataGridViewColumn.HeaderCell = new HeaderControl(dataGridViewColumn.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(dataGridViewColumn);
                    }
                    else if (((string) this.ColNames[i][2]) == "D")
                    {
                        DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
                        column2.DataPropertyName = (string) this.ColNames[i][0];
                        column2.HeaderText = (string) this.ColNames[i][1];
                        if (!this.SortByColumn)
                        {
                            column2.SortMode = DataGridViewColumnSortMode.NotSortable;
                        }
                        column2.HeaderCell = new HeaderControl(column2.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(column2);
                    }
                    else if (((string) this.ColNames[i][2]) == "N")
                    {
                        NumericColumn column3 = new NumericColumn();
                        column3.DataPropertyName = (string) this.ColNames[i][0];
                        column3.HeaderText = (string) this.ColNames[i][1];
                        column3.ZeroIsShown = false;
                        if (this.ColNames[i][3] != null)
                        {
                            column3.UserTextNumericFormat = (AppInstance.TextNumericFormat) this.ColNames[i][3];
                        }
                        else
                        {
                            column3.NumericPrecision = 10;
                            column3.NumericScaleOnFocus = 2;
                            column3.NumericScaleOnLostFocus = 2;
                        }
                        column3.HeaderCell = new HeaderControl(column3.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(column3);
                    }
                    else if (((string) this.ColNames[i][2]) == "L")
                    {
                        DataGridViewCheckBoxColumn column4 = new DataGridViewCheckBoxColumn();
                        column4.DataPropertyName = (string) this.ColNames[i][0];
                        column4.HeaderText = (string) this.ColNames[i][1];
                        column4.HeaderCell = new HeaderControl(column4.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(column4);
                    }
                }
            }
        }

        public virtual void OnFilter()
        {
        }

        public virtual void OnMerge()
        {
            if ((this.LocalTable != null) && (this.LocalTable.Rows.Count >= 2))
            {
                new FormMerge2Code(this.LocalTableName, this.LocalFieldName, this.LocalCurrentRow[this.LocalFieldName]).ShowDialog();
            }
        }

        public virtual void OnPreview()
        {
        }

        public virtual void OnPrint()
        {
        }

        public virtual void OnRefresh()
        {
            this.LocalGridBrowser.DataSource = new BindingSource(this.LocalDataSet, this.LocalTable.TableName);
        }

        [Browsable(false)]
        public void OnSetPosition(string ps_FieldName, object ps_SearchedText)
        {
            if (this.LocalTable != null)
            {
                int num = 0;
                BindingSource dataSource = this.LocalGridBrowser.DataSource as BindingSource;
                num = dataSource.Find(ps_FieldName, ps_SearchedText);
                if (num < 0)
                {
                    DataView view = new DataView(((DataSet) dataSource.DataSource).Tables[0], (this.FirstFilter.Equals(string.Empty) ? "" : (this.FirstFilter + " AND ")) + ps_FieldName + " LIKE '" + ps_SearchedText.ToString().Replace("'", "''") + "*'", "", DataViewRowState.OriginalRows);
                    if (view.Count > 0)
                    {
                        num = dataSource.Find(ps_FieldName, view[0][ps_FieldName]);
                    }
                }
                if (num >= 0)
                {
                    if (num == 0)
                    {
                        dataSource.Position = dataSource.Count - 1;
                        dataSource.Position = num;
                    }
                    else
                    {
                        dataSource.Position = num;
                    }
                }
            }
        }

        protected virtual void OnShowChildForm()
        {
        }

        protected virtual void OnShowChildForm(string ps_ChildItemCode)
        {
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F2:
                    if (this.ShowAddButton && this.btnAdd.Enabled)
                    {
                        if (this.AllowAddItem)
                        {
                            this.OnAdd();
                            break;
                        }
                        MessageBox.Show("Không có quyền thêm mới danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    break;

                case Keys.F3:
                    if (this.ShowEditButton && this.btnEdit.Enabled)
                    {
                        if (this.AllowEditItem)
                        {
                            this.OnEdit();
                        }
                        else
                        {
                            MessageBox.Show("Không có quyền sửa danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                    return true;

                case Keys.F4:
                    this.txtSearch.Focus();
                    return true;

                case Keys.F6:
                    if (this.ShowMergeButton && this.btnMerge.Enabled)
                    {
                        if (this.AllowMergeItem)
                        {
                            this.OnMerge();
                        }
                        else
                        {
                            MessageBox.Show("Không có quyền gộp mã!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                    return true;

                case Keys.F7:
                    this.OnPrint();
                    return true;

                case Keys.F8:
                    if (this.ShowDeleteButton && this.btnDelete.Enabled)
                    {
                        if (this.AllowDeleteItem)
                        {
                            this.OnDelete();
                        }
                        else
                        {
                            MessageBox.Show("Không có quyền xóa danh mục!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                    return true;

                case Keys.F10:
                    if (this.ShowDoneButton && this.btnDone.Enabled)
                    {
                        this.OnDone();
                    }
                    return true;

                case (Keys.Control | Keys.F7):
                    this.OnPreview();
                    return true;

                case Keys.Return:
                    if (!this.CheckLookup || this.IsParentForm)
                    {
                        if (this.IsParentForm)
                        {
                            this.OnShowChildForm();
                            return true;
                        }
                        goto Label_01F4;
                    }
                    if (this.EmptyIsValid || (this.LocalCurrentRow != null))
                    {
                        this.EnterKeyHook = true;
                        base.Close();
                    }
                    return true;

                case Keys.Escape:
                    if ((!this.CheckLookup && !this.CheckLookupChild) || ((this.CheckLookup && !this.CheckLookupChild) && this.IsChildForm))
                    {
                        base.Close();
                    }
                    this.EscapeKeyHook = true;
                    return true;

                default:
                    goto Label_01F4;
            }
            return true;
        Label_01F4:
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void SetFreeColumns()
        {
            if (this.LocalFieldCount > 0)
            {
                for (int i = 1; i <= this.LocalFieldCount; i++)
                {
                    DataGridViewTextBoxColumn dataGridViewColumn = new DataGridViewTextBoxColumn();
                    dataGridViewColumn.DataPropertyName = "Ma_Loai" + i.ToString();
                    dataGridViewColumn.HeaderText = "Mã loại " + i.ToString();
                    if (!this.SortByColumn)
                    {
                        dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
                    }
                    dataGridViewColumn.HeaderCell = new HeaderControl(dataGridViewColumn.HeaderCell);
                    this.LocalGridBrowser.Columns.Add(dataGridViewColumn);
                }
            }
        }

        internal void SetUserRights()
        {
            this.AllowAddItem = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "L", this.LocalTableName, 1);
            this.AllowEditItem = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "L", this.LocalTableName, 2);
            this.AllowDeleteItem = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "L", this.LocalTableName, 3);
            this.AllowMergeItem = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "L", this.LocalTableName, 4);
        }

        [Browsable(false), Category("Browser properties"), Description("Allow this user add new item."), DefaultValue(false)]
        public bool AllowAddItem
        {
            get
            {
                return this.ib_AllowAddItem;
            }
            set
            {
                this.ib_AllowAddItem = value;
            }
        }

        [DefaultValue(false), Browsable(false), Category("Browser properties"), Description("Allow this user delete current item.")]
        public bool AllowDeleteItem
        {
            get
            {
                return this.ib_AllowDeleteItem;
            }
            set
            {
                this.ib_AllowDeleteItem = value;
            }
        }

        [DefaultValue(false), Browsable(false), Category("Browser properties"), Description("Allow this user edit current item.")]
        public bool AllowEditItem
        {
            get
            {
                return this.ib_AllowEditItem;
            }
            set
            {
                this.ib_AllowEditItem = value;
            }
        }

        [Description("Allow this user to merge current item."), Browsable(false), DefaultValue(false), Category("Browser properties")]
        public bool AllowMergeItem
        {
            get
            {
                return this.ib_AllowMergeItem;
            }
            set
            {
                this.ib_AllowMergeItem = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public bool CheckLookup
        {
            get
            {
                return this.ib_CheckLookup;
            }
            set
            {
                this.ib_CheckLookup = value;
            }
        }

        [Browsable(false)]
        public bool CheckLookupChild
        {
            get
            {
                return this.ib_CheckLookupChild;
            }
            set
            {
                this.ib_CheckLookupChild = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public string ChildFirstFilter
        {
            get
            {
                return this.is_ChildFirstFilter;
            }
            set
            {
                if (value == null)
                {
                    value = string.Empty;
                }
                this.is_ChildFirstFilter = value;
            }
        }

        [Browsable(false)]
        public FormListLister ChildFormLister
        {
            get
            {
                return this.oChildFormLister;
            }
            set
            {
                this.oChildFormLister = value;
            }
        }

        [Browsable(false)]
        public string ChildItemCode
        {
            get
            {
                return this.is_ChildItemCode;
            }
            set
            {
                this.is_ChildItemCode = value;
                this.OnShowChildForm(this.is_ChildItemCode);
            }
        }

        [Browsable(false), Category("Browser properties")]
        public object[][] ColNames
        {
            get
            {
                return this.is_ColNames;
            }
            set
            {
                this.is_ColNames = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public string CurrentCode
        {
            get
            {
                return this.is_CurrentCode;
            }
            set
            {
                if (value == null)
                {
                    value = "";
                }
                if ((this.IsChildForm && (this.ParentCurrentRow == null)) && !value.Equals(string.Empty))
                {
                    DataTable table = AppDataAccess.SelectTable("SELECT TOP 1 * FROM " + this.LocalTableName + " WHERE UPPER(LEFT(" + this.LocalFieldName + ", " + value.Length.ToString() + ")) = '" + value.ToUpper() + "'");
                    if (table.Rows.Count > 0)
                    {
                        this.ParentFormLister.CurrentCode = table.Rows[0][this.ParentFieldName].ToString();
                        this.ParentCurrentRow = this.ParentFormLister.LocalCurrentRow;
                    }
                }
                this.is_CurrentCode = value;
                this.OnSetPosition(this.LocalFieldName, this.is_CurrentCode);
            }
        }

        [Browsable(false), Category("Browser properties")]
        public bool EmptyIsValid
        {
            get
            {
                return this.ib_EmptyIsValid;
            }
            set
            {
                this.ib_EmptyIsValid = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public bool EnterKeyHook
        {
            get
            {
                return this.ib_EnterKeyHook;
            }
            set
            {
                this.ib_EnterKeyHook = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public bool EscapeKeyHook
        {
            get
            {
                return this.ib_EscapeKeyHook;
            }
            set
            {
                this.ib_EscapeKeyHook = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public string FirstFilter
        {
            get
            {
                return this.is_FirstFilter;
            }
            set
            {
                if (value == null)
                {
                    value = string.Empty;
                }
                this.is_FirstFilter = value;
                ((BindingSource) this.LocalGridBrowser.DataSource).Filter = value;
            }
        }

        [Browsable(false)]
        public bool IsChildForm
        {
            get
            {
                return this.ib_IsChildForm;
            }
            set
            {
                this.ib_IsChildForm = value;
            }
        }

        [Browsable(false)]
        protected virtual bool IsContainChilds
        {
            get
            {
                return false;
            }
        }

        [Browsable(false)]
        public bool IsParentForm
        {
            get
            {
                return this.ib_IsParentForm;
            }
            set
            {
                this.ib_IsParentForm = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                try
                {
                    if ((this.LocalGridBrowser.CurrentRow == null) || (this.LocalGridBrowser.CurrentRow.DataBoundItem == null))
                    {
                        this.oCurrentRow = null;
                    }
                    else
                    {
                        this.oCurrentRow = ((DataRowView) this.LocalGridBrowser.CurrentRow.DataBoundItem).Row;
                    }
                }
                catch
                {
                    this.oCurrentRow = null;
                }
                return this.oCurrentRow;
            }
            set
            {
                this.oCurrentRow = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public DataSet LocalDataSet
        {
            get
            {
                return this.oLocalDataSet;
            }
            set
            {
                this.oLocalDataSet = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public int LocalFieldCount
        {
            get
            {
                if (this.ShowFreeFields && (this.ii_FieldCount == -1))
                {
                    try
                    {
                        this.ii_FieldCount = AppDataAccess.GetFreeFields(this.LocalTableName);
                    }
                    catch
                    {
                    }
                }
                return this.ii_FieldCount;
            }
            set
            {
                if (!string.IsNullOrEmpty(this.LocalTableName))
                {
                    this.ii_FieldCount = value;
                }
            }
        }

        [Category("Browser properties")]
        public string LocalFieldName
        {
            get
            {
                return this.is_FieldName;
            }
            set
            {
                this.is_FieldName = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public FormListEditor LocalFormEditor
        {
            get
            {
                return this.oFormEditor;
            }
            set
            {
                this.oFormEditor = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public GridControl LocalGridBrowser
        {
            get
            {
                return this.grdBrowser;
            }
        }

        [Category("Browser properties")]
        public string LocalKeyField
        {
            get
            {
                return this.is_LocalKeyField;
            }
            set
            {
                this.is_LocalKeyField = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public DataTable LocalTable
        {
            get
            {
                if ((this.LocalDataSet != null) && (this.LocalDataSet.Tables.Count != 0))
                {
                    return this.LocalDataSet.Tables[0];
                }
                return null;
            }
        }

        [Category("Browser properties")]
        public string LocalTableName
        {
            get
            {
                return this.is_LocalTableName;
            }
            set
            {
                if (this.is_LocalTableName != value)
                {
                    this.is_LocalTableName = value;
                    if (this.ShowFreeFields && AppDataAccess.CheckConnected)
                    {
                        this.SetFreeColumns();
                    }
                    else
                    {
                        this.LocalFieldCount = 0;
                    }
                    if (AppInstance.UserValiated)
                    {
                        this.SetUserRights();
                    }
                }
            }
        }

        [Category("Browser properties")]
        public string OrderFieldNames
        {
            get
            {
                return this.is_OrderFieldNames;
            }
            set
            {
                if (value.Equals(string.Empty))
                {
                    value = "";
                }
                this.is_OrderFieldNames = value;
            }
        }

        [Browsable(false)]
        public DataRow ParentCurrentRow
        {
            get
            {
                if (this.ParentFormLister != null)
                {
                    return this.ParentFormLister.LocalCurrentRow;
                }
                return null;
            }
            set
            {
                try
                {
                    if (value != null)
                    {
                        this.OnFillData();
                    }
                }
                catch
                {
                }
            }
        }

        [Browsable(false)]
        public string ParentFieldName
        {
            get
            {
                return this.ParentFormLister.LocalFieldName;
            }
        }

        [Browsable(false)]
        public FormListLister ParentFormLister
        {
            get
            {
                return this.oParentFormLister;
            }
            set
            {
                this.oParentFormLister = value;
            }
        }

        [Category("Browser properties")]
        public bool RefreshAfterChanged
        {
            get
            {
                return this.ib_RefreshAfterChanged;
            }
            set
            {
                this.ib_RefreshAfterChanged = value;
            }
        }

        [Category("Browser properties"), Description("Whether show the add button or hide."), DefaultValue(true)]
        public bool ShowAddButton
        {
            get
            {
                return this.ib_ShowAddButton;
            }
            set
            {
                if (this.ib_ShowAddButton != value)
                {
                    this.ib_ShowAddButton = value;
                    this.btnAdd.Visible = this.ShowAddButton;
                    this.tbrSep01.Visible = (this.ShowAddButton || this.ShowEditButton) || this.ShowDeleteButton;
                }
            }
        }

        [Category("Browser properties"), DefaultValue(true), Description("Whether show the delete button or hide.")]
        public bool ShowDeleteButton
        {
            get
            {
                return this.ib_ShowDeleteButton;
            }
            set
            {
                if (this.ib_ShowDeleteButton != value)
                {
                    this.ib_ShowDeleteButton = value;
                    this.btnDelete.Visible = this.ShowDeleteButton;
                    this.tbrSep01.Visible = (this.ShowAddButton || this.ShowEditButton) || this.ShowDeleteButton;
                }
            }
        }

        [DefaultValue(false), Description("Whether show the done button or hide."), Category("Browser properties")]
        public bool ShowDoneButton
        {
            get
            {
                return this.ib_ShowDoneButton;
            }
            set
            {
                if (this.ib_ShowDoneButton != value)
                {
                    this.ib_ShowDoneButton = value;
                    this.btnDone.Visible = this.ShowDoneButton;
                    this.tbrSep07.Visible = this.ShowDoneButton;
                }
            }
        }

        [DefaultValue(true), Description("Whether show the edit button or hide."), Category("Browser properties")]
        public bool ShowEditButton
        {
            get
            {
                return this.ib_ShowAditButton;
            }
            set
            {
                if (this.ib_ShowAditButton != value)
                {
                    this.ib_ShowAditButton = value;
                    this.btnEdit.Visible = this.ShowEditButton;
                    this.tbrSep01.Visible = (this.ShowAddButton || this.ShowEditButton) || this.ShowDeleteButton;
                }
            }
        }

        [Category("Browser properties"), Description("Whether show the free fields or hide."), DefaultValue(true)]
        public bool ShowFreeFields
        {
            get
            {
                return this.ib_ShowFreeFields;
            }
            set
            {
                if (this.ib_ShowFreeFields != value)
                {
                    this.ib_ShowFreeFields = value;
                }
            }
        }

        [Category("Browser properties"), Description("Whether show the merge button or hide."), DefaultValue(true)]
        public bool ShowMergeButton
        {
            get
            {
                return this.ib_ShowMergeButton;
            }
            set
            {
                if (this.ib_ShowMergeButton != value)
                {
                    this.ib_ShowMergeButton = value;
                    this.btnMerge.Visible = this.ShowMergeButton;
                    this.tbrSep04.Visible = this.ShowMergeButton;
                }
            }
        }

        [Category("Browser properties")]
        public bool SortByColumn
        {
            get
            {
                return this.ib_SortByColumn;
            }
            set
            {
                this.ib_SortByColumn = value;
            }
        }

        public object UserDefined1
        {
            get
            {
                return this.oUserDefined1;
            }
            set
            {
                this.oUserDefined1 = value;
            }
        }

        public object UserDefined2
        {
            get
            {
                return this.oUserDefined2;
            }
            set
            {
                this.oUserDefined2 = value;
            }
        }

        public object UserDefined3
        {
            get
            {
                return this.oUserDefined3;
            }
            set
            {
                this.oUserDefined3 = value;
            }
        }

        public object UserDefined4
        {
            get
            {
                return this.oUserDefined4;
            }
            set
            {
                this.oUserDefined4 = value;
            }
        }
    }
}

